﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Bricks.BaseFramework.Utility
{
    /*********************************************************
     * 1.版权：积木工场 Bricks Factory
     * 2.作用：字符串扩展方法
     * 3.创建人：罗泽新
     * 4.创建日期：2012-04-27
     * 5.最后修改人：罗泽新
     * 6.最后修改日期：2012-04-27
     * 7.修改项目：
     * *******************************************************/
    public class StringExtension
    {
        /// <summary>
        /// 解析字符串为字符列表
        /// </summary>
        /// <param name="listInString">待解析字符串</param>
        /// <param name="separator">分隔符</param>
        public static List<string> GenerateList(string listInString, string separator)
        {
            List<string> stringList = new List<string>();
            List<string> resultList = new List<string>();

            if (false == string.IsNullOrEmpty(listInString))
            {
                Regex regex = new Regex(separator);
                string[] substrings = regex.Split(listInString);
                stringList.AddRange(substrings);

                //过滤空字符串
                foreach (var item in stringList)
                {
                    if (item != string.Empty)
                    {
                        resultList.Add(item);
                    }
                }
            }
            return resultList;
        }

        /// <summary>合并Uri。
        /// 如uri1为http://www.163.com，uri2为db=4，合并后为http://www.163.com/db=4
        /// </summary>
        /// <param name="uri1">如http://www.163.com</param>
        /// <param name="uri2">db=4</param>
        public static string CombinUriString(string uri1, string uri2)
        {
            if (string.IsNullOrEmpty(uri1))
                return uri2;
            string newUri = uri1 + (uri1.EndsWith(@"/") ? "" : @"/") + uri2;
            return newUri;
        }

        /// <summary>
        /// 过滤文件后缀
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <returns>去掉文件后缀的字符串</returns>
        public static string FilterExtension(string fileName)
        {
            string name = string.Empty;
            if (false == string.IsNullOrWhiteSpace(fileName))
            {
                int idx = fileName.LastIndexOf(".");
                if (idx > -1)
                {
                    name = fileName.Substring(0, idx);
                }
            }
            return name;
        }
    }
}
